// cookie.js v1.0.0
// Copyright 2012 suckgamony@gmail.com

(function() {



// Beginning of code

var cached = null;

function trim(s) {
	return s.replace(/(^\s+|\s+$)/g, "");
}

function cache() {
	if (cached != null) return;
	cached = {};

	var cookies = document.cookie.split(";");

	for (var i = 0, l = cookies.length; i < l; ++i) {
		var keyValue = cookies[i].split("=");
		if (!keyValue[0]) continue;

		var k = decodeURIComponent(trim(keyValue[0]));
		if (k.length == 0) continue;

		var v = (keyValue[1] ? decodeURIComponent(trim(keyValue[1])) : null);

		cached[k] = v;
	}

	setTimeout(function() { cached = null; }, 1000);
}

function parseCookieName(name) {
	var path;
	var domain;

	var indexSlash = name.indexOf("/");
	if (indexSlash >= 0) {
		var lastIndexSlash = name.lastIndexOf("/");
		domain = name.substring(0, indexSlash);
		path = name.substring(indexSlash, lastIndexSlash);
		name = name.substr(lastIndexSlash + 1);
		if (!path) path = "/";
	}

	var result = {name: name};
	if (path) result.path = path;
	if (domain) result.domain = domain;

	return result;
}

function parseExpiration(expires) {
	if (!expires) {
		expires = 1000 * 365 * 24 * 60 * 60;
	}

	if (typeof(expires) == "number") {
		if (expires < 0) {
			return null;
		}
		else {
			return new Date(new Date().getTime() + expires * 1000).toUTCString();
		}
	}
	else {
		var m = /(?:([0-9]+):)?([0-9]+):([0-9]+)(?:\.([0-9]+))?/.exec(expires);
		if (m) {
			var h = (m[1] ? parseInt(m[1]) : 0);
			var min = (m[2] ? parseInt(m[2]) : 0);
			var s = (m[3] ? parseInt(m[3]) : 0);
			var ms = (m[4] ? parseInt(m[4]) : 0);

			var calculated = ((h * 60 + min) * 60 + s) * 1000 + ms;
			return new Date(new Date().getTime() + calculated).toUTCString();
		}
		else {
			return Date.parse(expires).toUTCString();
		}
	}
}

$cookie = function(name, value, expires) {
	if (arguments.length <= 1) {
		cache();
		return cached[name];
	}
	else {
		var parsed = parseCookieName(name);

		expires = parseExpiration(expires);

		document.cookie = encodeURIComponent(parsed.name) + "=" + encodeURIComponent(value) +
			(expires ? "; expires=" + expires : "") +
			(parsed.path ? "; path=" + parsed.path : "") +
			(parsed.domain ? "; domain=" + parsed.domain : "");

		if (cached) {
			cached[parsed.name] = value;
		}
	}
};

$cookie.get = function(___) {
	cache();
	
	var result = {};
	for (var i = 0, c = arguments.length; i < c; ++i) {
		var name = arguments[i];
		result[name] = cached[name];
	}

	return result;
};

$cookie.set = function(obj, expires) {
	for (var name in obj) {
		$cookie(name, obj[name], expires);
	}
};

$cookie.has = function(name) {
	cache();
	return (name in cached);
};

$cookie.remove = function(name) {
	var parsed = parseCookieName(name);
	document.cookie = encodeURIComponent(parsed.name) + "=; expires=Thu, 01-Jan-1970 00:00:01 GMT" +
		(parsed.path ? "; path=" + parsed.path : "") +
		(parsed.domain ? "; domain=" + parsed.domain : "");

	if (cached) {
		delete cached[parsed.name];
	}
};

$cookie.$each = function(proc, arg) {
	cache();
	if (typeof(proc) == "string") {
		var f = new Function("c", "a", "for (var n in c){if(!n)continue;var v=c[n];if((" + js.from.Lambda.replace(proc, "v", "n", "a") + ")===false)return false;}");
		return f(cached, arg);
	}
	else {
		for (var name in cached) {
			if (!name) continue;
			if (proc(cached[name], name, arg) === false) return false;
		}
	}
};

$cookie.clear = function() {
	var self = this;
	this.$each(function(v, k) {
		self.remove(k);
	});
};

$session = function(key, value) {
	$cookie(key, value, -1);
};

$session.set = function(obj) {
	for (var name in obj) {
		$cookie(name, obj[name], -1);
	}
};

// End of code



})();